<?php

namespace App\Transformers\Article;

use App\Models\Article;
use App\Models\MerchantRegister;
use App\Transformers\Category\EditTransformer;
use App\Transformers\Merchant\MerchantRegistersTransformer;
use App\Transformers\Product\ProductTransformer;
use App\Transformers\System\SystemAdminTransformer;
use League\Fractal\TransformerAbstract;

class ArticlesTransformer extends TransformerAbstract
{
    //可include的方法
    protected $availableIncludes = ['admin','product','categories','mer'];

    public function transform(Article $data){
        return [
            'id' => $data->id,
            'category_id' => $data->category_id ?? '',
            'admin_id' => $data->admin_id ?? '',
            'product_id' => $data->product_id ?? 0,
            'mer_id' => $data->mer_id ?? '',
            'title' => $data->title ?? '',
            'author' => $data->author ?? '',
            'image_input' => $data->image_input ?? '',
            'image_input_url' => 'https://placeimg.com/640/480/any',
            'synopsis' => $data->synopsis ?? '',
            'share_title' => $data->share_title ?? '',
            'share_synopsis' => $data->share_synopsis ?? '',
            'content' => $data->content ?? '',
            'visit' => $data->visit ?? '',
            'sort' => $data->sort ?? '',
            'url' => $data->url ?? '',
            'status' => $data->status ?? '',
            'is_hide' => $data->is_hide ?? '',
            'is_hot' => $data->is_hot  ?? '',
            'is_banner' => $data->is_banner ?? '',
            'created_at' => $data->created_at ? $data->created_at->format("Y-m-d H:i:s") : '',
            'updated_at' => $data->updated_at ? $data->updated_at->format("Y-m-d H:i:s") : '',
            'product' => $data->product ?? '',
        ];
    }


    /**
     * 额外的管理员数据
     */
    public function includeAdmin(Article $articles){
            return $this->item($articles->admin, new SystemAdminTransformer());
    }

    /**
     * 额外的分类数据
     */
    public function includecategories(Article $articles){

        return $this->item($articles->categories, new EditTransformer());

    }

    /**
     * 额外的商品数据
     */
    public function includeProduct(Article $articles){
        return $this->item($articles->product, new ProductTransformer());
    }

    /**
     * 额外的商户数据
     */
    public function includemer(Article $articles){
        return $this->item($articles->mer, new MerchantRegistersTransformer());
    }
}
